﻿(function() {

  $.rhd = $.rhd || {};

  $.rhd.grid = $.rhd.grid || {};

  $.extend($.rhd.grid, {
    version: "1.0"
  });

  $.rhd.grid.formatter = $.rhd.grid.formatter || {};

  $.extend($.rhd.grid.formatter, {
    string: function(val, opt) {
      if (!(val != null)) {
        return "";
      }
      return val.toString();
    },
    integer: function(val, opt) {
      if (!(val != null)) {
        return "";
      }
      return val.toString();
    },
    numeric: function(val, opt) {
      var numeric;
      if (!(val != null)) {
        return "";
      }
      numeric = new NumberFormat();
      numeric.setInputDecimal('.');
      numeric.setNumber(val);
      numeric.setPlaces(opt.decimalPlaces, true);
      numeric.setCurrency(false);
      numeric.setNegativeFormat(numeric.LEFT_DASH);
      numeric.setSeparators(true, opt.thousandsSeparator, opt.decimalSeparator);
      return numeric.toFormatted();
    },
    datetime: function(val, opt) {
      if (!(val != null)) {
        return "";
      }
      if ($.isNumeric(val) || typeof val === "string") {
        val = new Date(val);
      }
      return val.format(opt.mask);
    },
    boolean: function(val, opt) {
      if (val) {
        return $.rhd.grid.locale.yes;
      } else {
        return $.rhd.grid.locale.no;
      }
    }
  });

  $.rhd.grid.renderer = $.rhd.grid.renderer || {};

  $.extend($.rhd.grid.renderer, {
    _default: function(td, value, col, row, opt) {
      var span;
      if (col.textAlign != null) {
        td.css("text-align", col.textAlign);
      }
      span = $("<span>");
      span.appendTo(td);
      if (value && opt.maxLength > 0 && value.length > opt.maxLength) {
        span.prop("title", value);
        return span.text(value.substr(0, opt.maxLength) + "...");
      } else {
        return span.text(value);
      }
    },
    multiselect: function(td, value, col, row, opt) {
      var check,
        _this = this;
      check = $("<input>", {
        type: "checkbox",
        "class": "multiselect"
      });
      check.appendTo(td);
      if (row[col.dataField]) {
        check.get(0).checked = true;
        td.parent('tr').addClass("warning");
      }
      return check.change(function() {
        if (check.is(":checked")) {
          td.parent('tr').addClass("warning");
          row[col.dataField] = true;
        } else {
          td.parent('tr').removeClass("warning");
          row[col.dataField] = false;
        }
        return _this.updatedMultiSelect();
      });
    }
  });

  $.rhd.grid.header = $.rhd.grid.header || {};

  $.extend($.rhd.grid.header, {
    _default: function(th, col, opt) {
      var title,
        _this = this;
      if (col.width != null) {
        th.css("width", col.width);
      }
      if (col.textAlign != null) {
        th.css("text-align", col.textAlign);
      }
      title = $("<span>");
      title.append(col.title || "&nbsp;");
      title.append(this.makeIconSort(col));
      title.appendTo(th);
      if (col.tooltip != null) {
        title.prop("title", col.tooltip);
      }
      if ((col.sort != null) && col.sort.sortable) {
        title.css("cursor", "pointer");
        return title.click(function(event) {
          return _this.toggleSort(col, event.ctrlKey);
        });
      }
    },
    multiselect: function(th, col, opt) {
      var btnOptions, btnOptionsMenu, group,
        _this = this;
      th.prop("class", "multiselect");
      group = $("<div>", {
        "class": "btn-group"
      });
      group.appendTo(th);
      this.check = $("<input>", {
        type: "checkbox",
        "class": "multiselect"
      });
      this.check.appendTo(group);
      this.check.get(0).checked = this.pageChecked();
      this.check.change(function() {
        return _this.checkUncheckPage(_this.check.is(":checked"));
      });
      btnOptions = $("<a>", {
        "class": "dropdown-toggle",
        "data-toggle": "dropdown",
        href: "#"
      });
      btnOptions.append($("<span>", {
        "class": "caret"
      }));
      btnOptions.appendTo(group);
      btnOptionsMenu = $("<ul>", {
        "class": "dropdown-menu"
      });
      btnOptionsMenu.appendTo(group);
      btnOptionsMenu.append($("<li>").append($("<a>").append($.rhd.grid.locale.multiselect.checkAll).click(function() {
        return _this.checkAll();
      })));
      btnOptionsMenu.append($("<li>").append($("<a>").append($.rhd.grid.locale.multiselect.uncheckAll).click(function() {
        return _this.uncheckAll();
      })));
      return btnOptionsMenu.append($("<li>").append($("<a>").append($.rhd.grid.locale.multiselect.invert).click(function() {
        return _this.invert();
      })));
    }
  });

  $.rhd.grid.sorter = $.rhd.grid.sorter || {};

  $.extend($.rhd.grid.sorter, {
    string: function(a, b) {
      a = a ? new String(a).toLowerCase() : "";
      b = b ? new String(b).toLowerCase() : "";
      if (a === b) {
        return 0;
      }
      if (a < b) {
        return -1;
      }
      return 1;
    },
    integer: function(a, b) {
      a = parseFloat(a);
      if (isNaN(a)) {
        a = 0;
      }
      b = parseFloat(b);
      if (isNaN(b)) {
        b = 0;
      }
      return a - b;
    },
    numeric: function(a, b) {
      a = parseFloat(a);
      if (isNaN(a)) {
        a = 0;
      }
      b = parseFloat(b);
      if (isNaN(b)) {
        b = 0;
      }
      return a - b;
    },
    datetime: function(a, b) {
      a = a ? a : new Date();
      b = b ? b : new Date();
      if (a === b) {
        return 0;
      }
      if (a < b) {
        return -1;
      }
      return 1;
    },
    boolean: function(a, b) {
      a = a ? a : new Boolean();
      a = a.valueOf();
      b = b ? b : new Boolean();
      b = b.valueOf();
      if (a === b) {
        return 0;
      }
      if (a < b) {
        return -1;
      }
      return 1;
    }
  });

  $.rhd.grid.functions = $.rhd.grid.functions || {};

  $.extend($.rhd.grid.functions, {
    getValue: function(dataField, value) {
      if (!dataField || !value) {
        return null;
      }
      return value[dataField];
    }
  });

}).call(this);
